package com.woniuxy.yoga.user.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.user.model.form.SearchLoginLogForm;
import com.woniuxy.yoga.user.model.param.SearchLoginLogParam;
import com.woniuxy.yoga.user.service.UserLoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户登录日志表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/userLoginLog")
@Api(tags = "用户登录日志管理")
public class UserLoginLogController {

    @Autowired
    private UserLoginLogService userLoginLogService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 分页查询登录日志
     * @param form 参数
     * @return 分页数据
     */
    @PostMapping("/searchLoginLog.api")
    @ApiOperation("分页查询登录日志")
    @YogaLog("分页查询登录日志")
    public Result<PageInfo> searchLoginLog(@Valid @RequestBody SearchLoginLogForm form,@RequestHeader("authen_token") String token){
        Integer adminId = JWTUtil.getAdminId(redisUtil.get(token));
        SearchLoginLogParam param = BeanUtil.toBean(form, SearchLoginLogParam.class);
        param.setAdminId(adminId);
        PageInfo pageInfo = userLoginLogService.searchLoginLog(param);
        return Result.getResult(PageInfo.class).setMessage("查询登录日志成功").setData(pageInfo);
    }
}

